//
//  HYGetChildCompanyListApi.m
//  TJHYHardwareProduct
//
//  Created by JianWei Chen on 2018/1/23.
//  Copyright © 2018年 Kendama. All rights reserved.
//

#import "HYGetChildCompanyListApi.h"

@implementation HYGetChildCompanyListApi{
    NSDictionary *_dic;
}

-(id)initWithDic:(NSDictionary *)dic{
    if (self = [super init]) {
        _dic = dic;
    }
    return self;
}

- (NSString *)requestUrl {
    return @"/company/get_child_list";
}

- (YTKRequestMethod)requestMethod {
    return YTKRequestMethodPOST;
}

- (id)requestArgument {
    NSMutableDictionary *dic = [NSMutableDictionary getPubdic];
    
    [dic setValuesForKeysWithDictionary:_dic];
    NSString *sign=[NSString getSign:dic];
    [dic setObject:sign forKey:@"sign"];
    return dic;
}


-(void)dealloc
{
    NSLog(@"%@ dealloc",[self class]);
}

+ (void)getCompanyChildList:(int)pcid success:(void (^)(NSArray<HYChildCompanyModel *> *))successBack fail:(void (^)(NSDictionary *))failBack
{
    NSMutableDictionary *requestDic = [[NSMutableDictionary alloc]init];
    [requestDic setValue:@(pcid).stringValue forKey:@"pcid"];
    
    HYGetChildCompanyListApi *api = [[HYGetChildCompanyListApi alloc]initWithDic:requestDic];
    [api dtStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
        NSMutableArray *childList = [[NSMutableArray alloc]init];
        NSDictionary *dic = request.responseObject;
        if (dic[@"data"] && [dic[@"data"] isKindOfClass:[NSArray class]]) {
            NSArray *list = dic[@"data"];
            for (NSDictionary *infoDic in list) {
                HYChildCompanyModel *model = [[HYChildCompanyModel alloc]init];
                [model setValuesForKeysWithDictionary:infoDic];
                [childList addObject:model];
            }
        }
        if (successBack) {
            successBack(childList);
        }
    } failure:^(YTKBaseRequest * _Nonnull request) {
        if (failBack) {
            failBack(request.responseObject);
        }
    }];
}

+(void)getCompanyChildList:(int)pcid Province:(NSString *)province city:(NSString *)city district:(NSString *)district success:(void (^)(NSArray<HYChildCompanyModel *> *))successBack fail:(void (^)(NSDictionary *))failBack
{
    NSMutableDictionary *requestDic = [[NSMutableDictionary alloc]init];
    [requestDic setValue:@(pcid).stringValue forKey:@"pcid"];
    [requestDic setValue:province forKey:@"province"];
    [requestDic setValue:city forKey:@"city"];
    [requestDic setValue:district forKey:@"district"];
    
    HYGetChildCompanyListApi *api = [[HYGetChildCompanyListApi alloc]initWithDic:requestDic];
    [api dtStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
        NSMutableArray *childList = [[NSMutableArray alloc]init];
        NSDictionary *dic = request.responseObject;
        if (dic[@"data"] && [dic[@"data"] isKindOfClass:[NSArray class]]) {
            NSArray *list = dic[@"data"];
            for (NSDictionary *infoDic in list) {
                HYChildCompanyModel *model = [[HYChildCompanyModel alloc]init];
                [model setValuesForKeysWithDictionary:infoDic];
                [childList addObject:model];
            }
        }
        if (successBack) {
            successBack(childList);
        }
    } failure:^(YTKBaseRequest * _Nonnull request) {
        if (failBack) {
            failBack(request.responseObject);
        }
    }];
}
@end
